/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLNV.Action;

import QLNV.Bean.KhenThuongKyLuatForm;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;
import java.sql.*;
import QLNV.Bean.*;
import QLNV.Entity.*;

/**
 *
 * @author Ice
 */
public class KhenThuongKyLuatAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String DETAIL = "detail";
    private final static String VIEW = "view";

    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("khenthuongkyluat.insert", "insert");
        map.put("khenthuongkyluat.update", "update");
        map.put("khenthuongkyluat.delete", "delete");
        map.put("khenthuongkyluat.view", "view");
        map.put("khenthuongkyluat.search", "search");
        map.put("khenthuongkyluat.detail", "detail");
        return map;
    }

    /**
     * get KtklList
     * @param form
     * @return
     * @throws SQLException 
     */
    public ArrayList<KhenThuongKyLuat> getKtklList(ActionForm form) throws SQLException {
        KhenThuongKyLuatForm ktklForm = (KhenThuongKyLuatForm) form;
        return ktklForm.view();
    }

    /**
     * get nvList
     * @param form
     * @return
     * @throws SQLException 
     */
    public ArrayList<NhanVien> getNvList(ActionForm form) throws SQLException {
        NhanVienForm nvForm = (NhanVienForm) form;
        return nvForm.view();
    }

    /**
     * view
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward view(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        try {
            request.setAttribute("ktklList", getKtklList(form));
            request.setAttribute("nvList", getNvList(new NhanVienForm()));
            request.setAttribute("errorMessage", "1");
        } catch (Exception e) {
            e.getMessage();
        }
        return mapping.findForward(VIEW); //forward to 
    }

    /**
     * insert
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward insert(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        //ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
        //ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();

        KhenThuongKyLuatForm ktklForm = (KhenThuongKyLuatForm) form;
        //PhongBanForm pBanForm = (PhongBanForm) form;

        String maktkl = ktklForm.getMaktkl();
        String manv = ktklForm.getManv();
        String tenktkl = ktklForm.getTenktkl();
        String hinhthucktkl = ktklForm.getHinhthucktkl();
        String ngaythang = ktklForm.getNgaythang();

        //String message = nvForm.insert(manv, mapban, maluong, tennv, gioitinh, ngaysinh, diachi, email, noicongtac, chucvu);
        //nvList = nvForm.view();
        //pBanList = pBanForm.view();
        request.setAttribute("errorMessage", ktklForm.insert(maktkl, manv, tenktkl, hinhthucktkl, ngaythang));
        request.setAttribute("ktklList", getKtklList(form));
        request.setAttribute("nvList", getNvList(new NhanVienForm()));
        return mapping.findForward(VIEW);
    }

    /**
     * delete action
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        //ArrayList<KhenThuongKyLuat> ktklList = new ArrayList<KhenThuongKyLuat>();
        KhenThuongKyLuatForm ktklForm = (KhenThuongKyLuatForm) form;
        String maktkl = ktklForm.getMaktkl();
        int cnt = ktklForm.delete(maktkl);

        request.setAttribute("ktklList", getKtklList(form));
        request.setAttribute("nvList", getNvList(new NhanVienForm()));
        request.setAttribute("errorMessage", "Delete successfully");

        return mapping.findForward(VIEW);
    }

    /**
     * detail
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward detail(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        try {
            KhenThuongKyLuatForm ktklForm = (KhenThuongKyLuatForm) form;
            ArrayList<KhenThuongKyLuat> ktklList = new ArrayList<KhenThuongKyLuat>();
            ktklList = ktklForm.detail(ktklForm.getMaktkl());
            request.setAttribute("ktklList", ktklList);
            request.setAttribute("nvList", getNvList(new NhanVienForm()));
            request.setAttribute("errorMessage", "1");
        } catch (Exception e) {
            e.getMessage();
        }
        return mapping.findForward(DETAIL); //forward to PhongBanInfo.jsp
    }

    public ActionForward update(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws SQLException {
        KhenThuongKyLuatForm ktklForm = (KhenThuongKyLuatForm) form;
        String maktkl = ktklForm.getMaktkl();
        String manv = ktklForm.getManv();
        String tenktkl = ktklForm.getTenktkl();
        String hinhthucktkl = ktklForm.getHinhthucktkl();
        String ngaythang = ktklForm.getNgaythang();
        request.setAttribute("errorMessage", "1");
        request.setAttribute("ktklList", ktklForm.update(maktkl, manv, tenktkl, hinhthucktkl, ngaythang));
        request.setAttribute("nvList", getNvList(new NhanVienForm()));

        return mapping.findForward(DETAIL);
    }
    
    /**
     * search
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward search(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        ArrayList<KhenThuongKyLuat> ktklList = new ArrayList<KhenThuongKyLuat>();
        KhenThuongKyLuatForm ktklForm = (KhenThuongKyLuatForm) form;
        String maktkl = ktklForm.getMaktkl();
        ktklList = ktklForm.search(maktkl);
        request.setAttribute("ktklList", ktklList);
        if (!ktklList.isEmpty()) {
            request.setAttribute("errorMessage", "1");
            request.setAttribute("nvList", getNvList(new NhanVienForm()));
        } else {
            request.setAttribute("errorMessage", "Mã khen thưởng kỷ luật này không tồn tại.");
            request.setAttribute("nvList", getNvList(new NhanVienForm()));
        }
        return mapping.findForward(VIEW);
    }
    /* And your JSP would have the following format for submit buttons:
    
    <html:form action="/test">
    <html:submit property="method">
    <bean:message key="button.add"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.edit"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.delete"/>
    </html:submit>
    </html:form>
     */
}
